package com.source.sys.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Role;
import com.source.sys.entity.UserRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.source.sys.vo.UserRoleVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
public interface UserRoleMapper extends BaseMapper<UserRole> {

    /**
     * 显示UserRole
     * @return
     */
    @Select("select sys_user.userId,loginName,sys_role.rid as role_id, rname" +
            " from sys_user,sys_role,sys_user_role " +
            "where sys_user.userId = sys_user_role.userId" +
            " and sys_role.rid = sys_user_role.roleId" +
            " and isDelete = 0")
    Page<UserRoleVo> listUserRoleInfo(Page<UserRoleVo> userRoleVoPage);

    @Select("select sys_user.userId,loginName,sys_role.rid as role_id, rname " +
            "from sys_user,sys_role,sys_user_role " +
            "where sys_user.userId = sys_user_role.userId " +
            "and sys_role.rid = sys_user_role.roleId " +
            "and isDelete = 0 " +
            "and (sys_user.userId like concat('%',#{value},'%') " +
            "or loginName like concat('%',#{value},'%') " +
            "or roleId like concat('%',#{value},'%') " +
            "or rname like concat('%',#{value},'%') )")
    Page<UserRoleVo> likeSearchUserRolePage(@Param("value") String value,Page<UserRoleVo> userRoleVoPage);

    /**
     * 通过ID获得UserRole
     * @param userId
     * @return
     */
    @Select("select sys_user.userId,loginName,sys_role.rid role_id,rname" +
            " from sys_user,sys_role,sys_user_role" +
            " where sys_user.userId = sys_user_role.userId" +
            " and sys_role.rid = sys_user_role.roleId" +
            " and isDelete = 0" +
            " and useable = 1" +
            " and sys_user_role.userId=#{userId}")
    UserRoleVo getUserRoleInfoById(@Param("userId") String userId);

    /**
     * 通过用户id获取Role对象
     * @param userId
     * @return
     */
    @Select("select *" +
            " from sys_user,sys_role,sys_user_role" +
            " where sys_user.userId = sys_user_role.userId" +
            " and sys_role.rid = sys_user_role.roleId" +
            " and isDelete = 0" +
            " and useable = 1" +
            " and sys_user_role.userId=#{userId}")
    Role getRoleByUserId(@Param("userId") String userId);

}
